<template>

    <div v-loading="loading" style="margin-top: 40px">
        <el-button
                style="margin-left: 20px"
                @click="handleBackBook"
                type="primary"
                icon="el-icon-arrow-left"
                round>返回
        </el-button>
        <el-form ref="form" :model="list" label-width="100px" :rules="rules" style="margin-top: 24px">
            <el-form-item label="用户昵称">
                <el-input v-model="list.user_nickname" readonly="readonly" style="width: 300px"></el-input>
            </el-form-item>

            <el-form-item label="联系人" prop="name">
                <el-input v-model="list.name" style="width: 300px"></el-input>
            </el-form-item>

            <el-form-item label="手机号码" prop="mobile">
                <el-input v-model="list.mobile" style="width: 300px"></el-input>
            </el-form-item>

            <el-form-item label="性别" prop="sex">
                <el-select style="width: 300px" v-model="list.sex">
                    <el-option
                            v-for="item in sexData"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="用餐人数" prop="people">
                <el-input v-model="list.people" style="width: 300px"></el-input>
            </el-form-item>

            <el-form-item label="用餐时间" prop="book_time">
                <el-input v-model="list.book_time" style="width: 300px"></el-input>
            </el-form-item>

            <el-form-item label="位置" prop="position">
                <el-select style="width: 300px" v-model="list.position">
                    <el-option
                            v-for="item in seatList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="备注">
                <el-input v-model="list.remark" style="width: 300px"></el-input>
            </el-form-item>

            <el-button
                    @click="handleSave"
                    style="margin-left: 50px;margin-top: 24px"
                    type="primary">保存
            </el-button>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: 'bookOrderInfo',
        data() {
            return {
                list: {},
                seatList: [],
                loading: false,
                sexData: [
                    {id: 0, name: '女'},
                    {id: 1, name: '男'}
                ],
                rules: {
                    name: [
                        { required: true, message: '请输入联系人', trigger: 'blur' },
                    ],
                    mobile: [
                        { required: true, message: '请输入手机号码', trigger: 'blur' },
                    ],
                    sex: [
                        { required: true, message: '请选择性别', trigger: 'blur' },
                    ],
                    people: [
                        { required: true, message: '请输入用餐人数', trigger: 'blur' },
                    ],
                    book_time: [
                        { required: true, message: '请输入用餐时间', trigger: 'blur' },
                    ],
                    position: [
                        { required: true, message: '请选择位置', trigger: 'blur' },
                    ],
                }
            }
        },
        methods: {
            defaultData() {
                this.loading = true;
                this.$http.get('/store/book/read', {
                    params: {
                        token: localStorage.getItem('user_token'),
                        id: this.$route.query.id
                    }
                }).then(response => {
                    if (response.data.status === 'success') {
                        this.list = response.data.data;
                        this.list.token = localStorage.getItem('user_token');
                    }
                    this.loading = false;
                }).catch(error => {
                    console.log(error);
                    this.loading = false;
                })
            },

            //获取位置信息
            getSeatData() {
                this.loading = true;
                this.$http.get('/store/seat/position', {
                    params: {
                        token: localStorage.getItem('user_token'),
                    }
                }).then(response => {
                    if (response.data.status === 'success') {
                        this.seatList = response.data.data;
                    }
                    this.loading = false;
                }).catch(error => {
                    console.log(error);
                    this.loading = false;
                });

            },

            handleSave(){
                this.$refs['form'].validate((valid) => {
                    if (!valid) {
                        return false;
                    } else {
                        this.loading = true;
                        this.$http.post('/store/book/update', this.list).then(response => {
                            if (response.data.status === 'success'){
                                this.$message.success('保存成功');
                                this.handleBackBook();
                            }
                            this.loading = false;
                        }).catch( () => {
                            this.loading = false;
                        })
                    }
                });

            },

            handleBackBook(){
                this.$router.push({name: 'orderBook'})
            }
        },
        mounted() {
            this.getSeatData();
            this.defaultData();
        }
    }
</script>
